
import api from './api'

let Regular = {
  makeUp:function (val,option,url,isEdit,filters,isProduct,labelList) {

    if(val.length>option.length){
        //增加
        val.forEach(function(item,index,map){
          let isExist = false
          let valuetype = '';
          if(labelList&&labelList.length){
            labelList.forEach(function (li,i) {
              if(li.dimNo ==item.value ){
                valuetype = li.isMulti
              }
            })
          }


          if(option.length){
            option.forEach(function (unit,i) {
              if(item.value == unit.dimNo){
                isExist = true
              }
            })


            if(!isExist){
              let itemObj={
                dimNo:item.value,
                dimNm:item.label,
                isMulti:valuetype,
                type:item.type || 'contain',
                valueList:[],
                resultList:[]
              }

              //resultList
              //标签项列表
              if(isProduct){
                api({
                  url: url,
                  method: "post",
                  params:{
                    attrId:item.value
                  }
                }).then(data => {
                  itemObj.resultList = data;
                })
              }else{
                api({
                  url: url,
                  method: "post",
                  params:{
                    dimNo:item.value
                  }
                }).then(data => {
                  itemObj.resultList = data;
                })
              }

              option.push(itemObj)
            }

          }else {
            let itemObj={
              dimNo:item.value,
              dimNm:item.label,
              isMulti:valuetype,
              type:item.type || 'contain',
              valueList:[],
              resultList:[]
            }

            //resultList
            //标签项列表
            if(isProduct){
              api({
                url: url,
                method: "post",
                params:{
                  attrId:item.value
                }
              }).then(data => {
                itemObj.resultList = data;
                if(isEdit&&filters){
                  filters.forEach(function (li) {
                    if(item.value == li.fieldNo){
                      itemObj.type = li.type
                      itemObj.valueList = li.valueList

                    }
                  })
                }
                option.push(itemObj)
              })
            }else{
              api({
                url: url,
                method: "post",
                params:{
                  dimNo:item.value
                }
              }).then(data => {
                itemObj.resultList = data;
                if(isEdit&&filters){
                  filters.forEach(function (li) {
                    if(item.value == li.fieldNo){
                      itemObj.type = li.type
                      itemObj.valueList = li.valueList
                    }
                  })
                }
                option.push(itemObj)
              })
            }


          }

        })
      }else if(val.length<option.length){
        //减少
      if(val&&val.length == 0){
        option=[]
      }else{
         option.forEach(function (item,index) {
              let flag = false
              val.forEach(function (unit,i) {
                if(item.dimNo == unit.value){
                  flag = true
                }
              })

              if(!flag){
                option.splice(index, 1);
              }
          })
      }

      }else{
        //不改变个数 只改变内容
        // console.log('danger')
      }


  },
  toRegular:function(a,b){
    //生成正则字符串
    //判断传输数据是否正常
    if(a&&a.filterType&&b&&b.filters){
      //正常执行
    }else{
      return 'err:a/b err';
    }

    //获取所有关系
    var gx=(/^and$/gi).test(a.filterType)?'1':(/^or$/gi).test(a.filterType)?'2':'';


    //获取单个标签项关系
    var bq=b.filters,sj={},s,px=[];
    for(var i=0,j=0;i<bq.length;i++){
      s=bq[i];
      if(s.fieldNo&&s.valueList&&s.type){
        //取出标签的数据顺序
        s.sx=s.fieldNo
        s.sx=(s.sx+'0');	//增加字段长度
        s.sx=s.sx.replace(/_w0$/gi,'1');	//把包含_w0包含替换为1
        s.sx=s.sx.replace(/\D/gi,'');	//转化为字符串类型的数字
        s.sx=parseInt(s.sx,10);	//转化为数字
        s.fieldNo;	//客户属性ID
        s.valueList;	//属性值
        s.type;	//关系
        px.push(s.sx);	//添加到排序中
        sj[s.sx+'']=s;	//取出了所有数据
      }else{
        return 'err:b';
      }
    }
    //对数据排序

    function sortNumber(a,b)
    {
      return a - b
    }

    px=px.sort(sortNumber);

    //输出单个正则
    function Reg(dx){
      var reg=['(\\d{1,10},)*','(,*)(\\d{1,10},)*','(,*)(\\d{1,10},)*(\\d{1,10})*;'],returnReg;	//客户标签值为2-10个数字
      var duoshuxin=[],orReg;	//单个或的关系
      switch (dx.type){
        case 'equal':
          returnReg=dx.valuetype=='1'?(dx.fieldNo+':'+reg[0]+'('+dx.valueList+')'+reg[2]):(dx.fieldNo+':'+dx.valueList+';');		//一个产品属性
          break;
        case  'contain':
          orReg=dx.valueList.join('|');
          returnReg=dx.valuetype=='1'?(dx.fieldNo+':'+reg[0]+'('+orReg+')'+reg[2]):(dx.fieldNo+':'+'('+orReg+')'+';');		//一个产品属性
          break;
        default:
          return ''
      }
      return returnReg
    }
    var shuchu=[];
    for (var n=0;n<px.length;n++ ){
      var dx=sj[px[n]];
      shuchu[n]='('+Reg(dx)+')'
      //console.log(dx + ' : '+Reg(dx));
    }

    return gx=='2'?shuchu.join('|'):shuchu.join('(.*)');
  },
  toJson:function(z){
    var gx=z.indexOf(')|(')>-1?"or":z.indexOf('(.*)')>-1?"and":"and",zn=[],zh=[];	//判断整体的关系

//拆分多个字段
//z=gx=='or'?z.replace(/((;\)\|\())/g,';d'):gx=='and'?z.replace(/((;\)\(\.\*\)\())/g,';d'):'and';	//替换
    z=gx=='or'?z.replace(/((;\)\|\())/g,';'):gx=='and'?z.replace(/((;\)\(\.\*\)\())/g,';'):'and';	//替换
    z=z.substring(1,z.length-2);	//截取出要拆分的字段
    zn=z.split(';');

    for (var i=0;i<zn.length;i++){
      var chaifen1=zn[i].split(':'),dx={};;
      dx.fieldNo=chaifen1[0];
      dx.type=chaifen1[1].indexOf('|')>-1?'contain':'equal';	//判断前端选项
      chaifen1[1].indexOf('d{1')>-1&&(dx.valuetype='1');	//判断是单值还是多值
      chaifen1[2]=chaifen1[1].replace(/(\(,\*\)\(\\d\{1\,10\}\,\)\*\(\\d\{1\,10\}\)\*)|(\(\\d\{1\,10\}\,\)\*)/g,'');
      dx.valueList=chaifen1[2].replace(/\(|\)/g,'').split('|');
      dx.valueList.length==1&&chaifen1[1].indexOf('(')>-1&&(dx.type='contain');
      dx.type=='equal'&&(dx.valueList=dx.valueList[0]);	//如果是dx.type=equal，dx.valueList为字符串
      //console.log(dx);
      zh.push(dx);
    }

    return {'filterType':gx,'filters':zh}
  }
}

export default Regular

